import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'forecast', //现金流预测
  state: {
    selectData: {},
    accountData: { inflowAmt: undefined, outflowAmt: undefined },
  },
  effects: {
    //净现金流（柱状图）
    *netCash({ payload, callback }, { call, put, select }) {
      const res = yield call(api.forecast.netCash, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询净现金流失败!');
      }
    },

    //频率统计值（表格）
    *frequencyCount({ payload, callback }, { call, put, select }) {
      const res = yield call(api.forecast.frequencyCount, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询频率统计值失败!');
      }
    },

    //获取汇总信息
    *getAccount({ payload }, { call, put }) {
      const res = yield call(api.forecast.getAccount, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            accountData: res.data,
          },
        });
      } else {
        message.error('查询总计信息失败');
      }
    },

    //现金流明细（表格）
    *cashFlowDetails({ payload, callback }, { call, put, select }) {
      const res = yield call(api.forecast.cashFlowDetails, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg || '查询现金流明细失败!');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
